package com.qipay.sp.excutor.huifu;

import com.alibaba.fastjson.JSONObject;
import com.qipay.baas.model.Order;
import com.qipay.order.config.OrderStateEnum;
import com.qipay.order.service.OrderService;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class HfAliNativeExcutor extends AbstractExcutor {

    private final static String API_URL="api_url";
    @Autowired
    OrderService orderService;

    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.ALIPAY_WAP;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put("merchant_order_no",order.getTradeNo());
        apiParams.put("pay_type","1");//1：支付宝H5
        apiParams.put("trade_amount",
                new BigDecimal(order.getAmount()).divide(new BigDecimal("100"),2,BigDecimal.ROUND_DOWN).toString());
        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {
        Object apiUrl = getVal(spconfig, API_URL);
        Object merchantNo = getVal(spconfig, "merchant_no");//商户uid
        Object notifyUrl= getVal(spconfig, "notify_url");
        Object returnUrl= getVal(spconfig, "return_url");
        Object key= getVal(spconfig, "key");

        params.put("merchant_no",merchantNo);
        params.put("notify_url",notifyUrl);
        params.put("return_url",returnUrl);

        String sign = MD5Util.getSign(params,key.toString());
        params.put("sign",sign);

        String result = sendPostByKeyValue(params,apiUrl.toString());

        if (StringUtils.isBlank(result)) {
            throw new Exception("请求辉付的微信接口失败");
        }
        Order order = orderService.getByTradeNo(params.get("merchant_order_no").toString());
//        order.setRemark(result);

        Map<String, Object> resultMap = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(result);
        if("1".equals(jsonObject.getString("status"))){
            Map<String, Object> responseMap = JSONObject.parseObject(jsonObject.getString("data"));
            resultMap.put("result_code", "0");
            resultMap.put("code_url", responseMap.get("pay_url"));
            order.setState(OrderStateEnum.WAITING.getType());
        }else{
            resultMap.put("result_code", "1");
            resultMap.put("message", jsonObject.getString("info"));
            order.setState(OrderStateEnum.REQERROR.getType());
        }
        orderService.update(order);
        return resultMap;
    }

}
